define(function(required, exports, modules) {
  var modal = require("mods/modal");
  exports.user_down = function(ols) {
    $("." + ols).dropdown({
      on: "hover"
    });
  };
  exports.user_set = function() {
    $(".selection").change(function() {
      var role = $("input[name=user-role]").val();
      if (role == "4") {
        $(".role_pid").show();
      } else {
        $(".role_pid").hide();
      }
    });

    $(".user-save").click(function() {
      if ($(this).hasClass("disabled")) {
        modal.alert("系统中在处理中~~~,请不要重复请求");
        return false;
      }

      var user_name = $("input[name=user-name]").val();
      var user_ID = $("input[name=user-ID]").val();
      var user_phone = $("input[name=user-phone]").val();
      var user_role = $("input[name=user-role]").val();
      var user_id = $("input[name=user-id]").val();
      var role_pid = $("input[name=role-pid]").val();
      var _csrf = $("#csrf").val();

      if (user_name.length < 1) {
        modal.msg("人员名称不能为空");
        return false;
      }

      if (user_ID.length < 1 || isCardNo(user_ID) == false) {
        modal.msg("请填写正确的身份证号");
        return false;
      }

      if (user_phone.length < 1 && !validatePhone(user_phone)) {
        modal.msg("请填写正确的电话号码");
        return false;
      }

      if (user_role.length < 1) {
        modal.msg("人员身份不能为空");
        return false;
      }

      $(this).addClass("disabled");

      $.ajax({
        url: "/admin/user/set",
        type: "POST",
        dataType: "json",
        data: {
          user_name: user_name,
          user_role: user_role,
          user_ID: user_ID,
          user_id: user_id,
          role_pid: role_pid,
          user_phone: user_phone,
          _csrf: _csrf
        },
        success: function(res) {
          $(this).removeClass("disabled");
          if (res.code == 200) {
            modal.msg(res.msg, {}, function() {
              window.location.href = "/admin/user/index";
            });
          } else {
            modal.alert(res.msg);
            return false;
          }
        }
      });
    });
  };

  function validatePhone(phone) {
    //验证规则
    var isMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
    var isPhone = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
    //如果为1开头则验证手机号码
    if (mobile.substring(0, 1) == 1) {
      if (!isMobile.exec(mobile) && mobile.length != 11) {
        //错误提示信息
        return false;
      }
    }
    //如果为0开头则验证固定电话号码
    // else if (mobile.substring(0, 1) == 0) {
    //     if (!isPhone.test(mobile)) {
    //         //错误提示信息
    //         return false;
    //     }
    // }
    //否则全部不通过
    else {
      //错误提示信息
      return false;
    }
    return true;
  }

  // 验证身份证
  function isCardNo(card) {
    var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return pattern.test(card);
  }
});
